home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / WarpQuake / Src / amiga_ppc_mathlib.s < prev    next >
Text File  |  2000-05-22  |  3KB  |  143 lines

  1. # Copyright (C) 2000 Peter McGavin.
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
  11. #
  12. # See the GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  17.  
  18. ########################################################################
  19.         .section    ".text"
  20.  
  21.         .align    2
  22.         .globl    Length
  23.         .globl    _Length
  24.         .type    Length,@function
  25.         .type    _Length,@function
  26.  
  27. Length:
  28. _Length:
  29.         lfs    f0,4(r3)    # f0 = v[1]
  30.         fmuls    f0,f0,f0    # f0 = v[1]*v[1]
  31.         lfs    f13,0(r3)    # f13 = v[0]
  32.         lfs    f12,8(r3)    # f12 = v[2]
  33.         fmadds    f13,f13,f13,f0    # f13 = v[0]*v[0] + f0
  34.         fmadds    f1,f12,f12,f13    # f1 = v[2]*v[2] + f13
  35. #        b    sqrt
  36. .Length_end:
  37.         .size            Length,.Length_end-Length
  38.  
  39. # No blr !!   Drop thru to sqrt !!
  40.  
  41. ########################################################################
  42.         .section    ".rodata"
  43.         .align    2
  44. .consts:
  45.         .long    0x3f000000    # 0.5
  46.         .long    0x3f800000    # 1.0
  47.  
  48.         .section    ".text"
  49.  
  50.         .align    2
  51.         .globl    sqrt
  52.         .globl    _sqrt
  53.         .type    sqrt,@function
  54.         .type    _sqrt,@function
  55.  
  56. sqrt:
  57. _sqrt:
  58.  
  59. # Returns approx sqrt(f1) in f1.
  60. # Accurate to about 1 part in 1000.
  61. # Preserve the following registers: r3, r4, f10, f11, f12
  62. # Also return 0.0 in f2 and 1.0 in f3
  63.  
  64.         fsubs    f2,f1,f1    # f2 = 0.0
  65.         lis    r9,.consts@ha
  66.         fcmpu    cr0,f1,f2    # f1 <= 0.0?
  67.         la    r9,.consts@l(r9)
  68.         ble-    .end
  69.         frsqrte    f9,f1        # f9 ~ 1.0/sqrt(f1)
  70.         lfs    f3,4(r9)    # f3 = 1.0
  71.         lfs    f7,0(r9)    # f7 = 0.5
  72.         fdivs    f8,f3,f9    # f8 = 1.0 / f9
  73.         fmadds    f9,f1,f9,f8    # f9 = f1 * f9 + f8
  74.         fmuls    f1,f7,f9    # f9 *= 0.5
  75.  
  76. #        fmuls    f9,f7,f9    # f9 *= 0.5
  77. #        fmuls    f8,f7,f9    # f8 = 0.5 * f9
  78. #        fdivs    f9,f1,f9    # f9 = f1 / f9
  79. #        fmadds    f1,f7,f9,f8    # f1 = 0.5 * f9 + f8
  80. .end:
  81.         blr
  82. .sqrt_end:
  83.         .size            sqrt,.sqrt_end-sqrt
  84.  
  85. ########################################################################
  86.         .section    ".rodata"
  87.         .align    2
  88. .one:
  89.         .long    0x3f800000    # 1.0
  90.  
  91.         .section    ".text"
  92.  
  93.         .align    2
  94.         .globl    VectorNormalize
  95.         .globl    _VectorNormalize
  96.         .type    VectorNormalize,@function
  97.         .type    _VectorNormalize,@function
  98.  
  99. VectorNormalize:
  100. _VectorNormalize:
  101.  
  102. # float VectorNormalize (vec3_t v)
  103. # {
  104. #   float length, ilength;
  105. #
  106. #   length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  107. #   length = sqrt (length);
  108. #   if (length) {
  109. #     ilength = 1/length;
  110. #     v[0] *= ilength;
  111. #     v[1] *= ilength;
  112. #     v[2] *= ilength;
  113. #   }
  114. #   return length;
  115. # }
  116.  
  117.         mflr    r4
  118.  
  119.         lfs    f10,0(r3)    # f10 = v[0]
  120.         lfs    f11,4(r3)    # f11 = v[1]
  121.         lfs    f12,8(r3)    # f12 = v[2]
  122.         fmuls    f1,f10,f10    # f1 = v[0]*v[0]
  123.         fmadds    f1,f11,f11,f1    # f1 = v[1]*v[1] + f1
  124.         fmadds    f1,f12,f12,f1    # f1 = v[2]*v[2] + f1
  125.  
  126.         bl    sqrt        # f1 = sqrt(f1), f2 = 0.0, f3 = 1.0
  127.         mtlr    r4
  128.  
  129.         fcmpu    cr0,f1,f2
  130.         ble-    .skip
  131.  
  132.         fdivs    f0,f3,f1    # ilength = 1.0 / length
  133.         fmuls    f10,f10,f0    # f10 *= ilength
  134.         fmuls    f11,f11,f0    # f11 *= ilength
  135.         fmuls    f12,f12,f0    # f12 *= ilength
  136.         stfs    f10,0(r3)    # v[0] = f10
  137.         stfs    f11,4(r3)    # v[1] = f11
  138.         stfs    f12,8(r3)    # v[2] = f12
  139. .skip:
  140.         blr
  141. .VectorNormalize_end:
  142.         .size            VectorNormalize,.VectorNormalize_end-VectorNormalize
  143.